package com.cango.qutz.web.auth.role.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cango.qutz.web.auth.role.dao.IRoleDao;
import com.cango.qutz.web.entity.MenuButtonEntity;
import com.cango.qutz.web.entity.RoleEntity;
import com.cango.qutz.web.entity.RoleResourceEntity;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.framework.base.pagination.PaginationResult;
import com.cango.qutz.web.auth.role.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cango.qutz.web.auth.role.service.IRoleService;

@Service("roleService")
public class RoleServiceImpl implements IRoleService {
    /** 角色sql的命名空间 */
    private static final String SQL_NAMESPACE = "role.";

    /** 新增角色的sql id */
    private static final String SQL_INSERT = "insertRole";

    /** 更新角色的sql id */
    private static final String SQL_UPDATE = "updateRole";

    /** 删除角色的sql id */
    private static final String SQL_DELETE = "deleteRole";

    /** 根据主键查询角色的sql id */
    private static final String SQL_QUERY_BY_ID = "queryRoleById";

    /** 查询角色的sql id */
    private static final String SQL_QUERY_FOR_LIST = "queryRoles";

    /** 分页查询角色的sql id */
    private static final String SQL_QUERY_BY_PAGE = "queryRolesByPage";

    /** 根据角色代码查询角色数量的sql id */
    private static final String SQL_QUERY_COUNT_BY_CODE = "queryCountByCode";

    /** 根据岗位代码查询角色信息的sql id */
    private static final String SQL_QUERY_POSITION_ROLES = "queryPositionRoles";

    /** 查询所有有效的菜单按钮信息的sql id */
    private static final String SQL_SELECT_MENU_LIST = "selectMenuList";

    /** 查询所有有效的菜单按钮信息的sql id */
    private static final String SQL_SELECT_BUTTON_LIST = "selectButtonList";

    /** 新增角色与资源关系信息的sql id */
    private static final String SQL_INSERT_ROLE_RESOURCE = "insertRoleResource";

    /** 删除角色与资源关系信息的sql id */
    private static final String SQL_DELETE_ROLE_RESOURCE = "deleteRoleResource";

    @Autowired
    private IRoleDao roleDao;

    @Override
    public int insert(RoleEntity role, String resources) {
        role.setIsDelete(0);
        role.setStatus(1);
        // 新增角色
        int id = this.roleDao.insert(SQL_NAMESPACE + SQL_INSERT, role);
        //新增角色与资源关系
        this.insertRoleResource(role, resources);
        return id;
    }

    /**
     * 功能描述: <br>
     * 新增角色与资源关系
     *
     * @param role
     * @param resources
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void insertRoleResource(RoleEntity role, String resources) {
        if (null != resources && !"".equals(resources)) {
            // 添加角色和资源的关系
            List<RoleResourceEntity> resList = new ArrayList<RoleResourceEntity>();
            String[] params = resources.split(",");
            for (int i = 0; i < params.length; i++) {
                RoleResourceEntity roleRes = new RoleResourceEntity();
                roleRes.setRoleCode(role.getRoleCode());
                roleRes.setCreateUser(role.getCreateUser());
                roleRes.setCreateTime(new Date());
                String[] typeArr = params[i].split(";");
                roleRes.setResourceCode(typeArr[0]);
                roleRes.setResourceType(typeArr[1]);
                resList.add(roleRes);
            }
            this.roleDao.insert(SQL_NAMESPACE + SQL_INSERT_ROLE_RESOURCE, resList);
        }
    }

    @Override
    public int update(RoleEntity roleEdit, String resources) {
        // 修改角色的信息
        RoleEntity role = this.queryObjectById(roleEdit.getRoleId());
        roleEdit.setRoleCode(role.getRoleCode());
        roleEdit.setCreateUser(role.getCreateUser());
        roleEdit.setCreateTime(role.getCreateTime());
        roleEdit.setIsDelete(role.getIsDelete());
        roleEdit.setStatus(role.getStatus());
        int id = this.roleDao.update(SQL_NAMESPACE + SQL_UPDATE, roleEdit);
        // 修改角色与资源关系信息 先删 后新增
        this.roleDao.delete(SQL_NAMESPACE + SQL_DELETE_ROLE_RESOURCE, roleEdit.getRoleCode());
        this.insertRoleResource(roleEdit, resources);
        return id;
    }

    @Override
    public int delete(RoleEntity role) {
        return roleDao.delete(SQL_NAMESPACE + SQL_DELETE, role);
    }

    @Override
    public RoleEntity queryObjectById(Integer roleId) {
        return roleDao.queryObjectById(SQL_NAMESPACE + SQL_QUERY_BY_ID, roleId);
    }

    @Override
    public PaginationResult<RoleEntity> queryListByPage(RoleEntity param, PageInfo pageinfo) {
        return roleDao.queryListByPage(SQL_NAMESPACE + SQL_QUERY_BY_PAGE, param, pageinfo);
    }

    @Override
    public PaginationResult<RoleEntity> queryRoleList(RoleEntity param, PageInfo pageinfo) {
        return roleDao.queryListByPage(SQL_NAMESPACE + SQL_QUERY_FOR_LIST, param, pageinfo);
    }

    @Override
    public PaginationResult<RoleEntity> queryPositionRoleList(RoleEntity param, PageInfo pageinfo) {
        return roleDao.queryListByPage(SQL_NAMESPACE + SQL_QUERY_POSITION_ROLES, param, pageinfo);
    }

    @Override
    public boolean validCode(String roleCode) {
        int count = this.roleDao.queryCountByCode(SQL_NAMESPACE + SQL_QUERY_COUNT_BY_CODE, roleCode);
        return count > 0 ? false : true;
    }

    @Override
    public List<MenuButtonEntity> queryMenuButtonList(String roleCode) {
        List<MenuButtonEntity> list = new ArrayList<MenuButtonEntity>();
        list = this.roleDao.queryList(SQL_NAMESPACE + SQL_SELECT_MENU_LIST, roleCode);
        List<MenuButtonEntity> listBut = this.roleDao.queryList(SQL_NAMESPACE + SQL_SELECT_BUTTON_LIST, roleCode);
        list.addAll(listBut);
        return list;
    }
}
